package com.longjianghuasheng.logistics.business.freight

import android.os.Bundle
import android.view.View
import coil.load
import com.longjianghuasheng.logistics.BR
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseFragment
import com.longjianghuasheng.logistics.databinding.FragmentFleetOrEnterpriseBinding
import com.longjianghuasheng.logistics.eventViewModel
import com.longjianghuasheng.logistics.ext.observer
import com.longjianghuasheng.logistics.ext.onClickIsLogin
import com.longjianghuasheng.logistics.ext.selectPicture
import me.hgj.jetpackmvvm.ext.view.invisible
import me.hgj.jetpackmvvm.ext.view.visible

/**
 * 车队合作
 */
class FleetOrEnterpriseFragment :
    BaseFragment<CooperateViewModel, FragmentFleetOrEnterpriseBinding>() {
    private val cardFrontListener = { _: View ->
        selectPicture {

            mViewModel.card_front = it.realPath

            mDatabind.ivCardFront.run {
                visible()
                load(it.path)
            }


        }

    }
    private val cardBackListener = { _: View ->
        selectPicture {

            mViewModel.card_back = it.realPath

            mDatabind.ivCardBack.run {
                visible()
                load(it.path)
            }


        }

    }
    private val licenseListener = { _: View ->
        selectPicture {

            mViewModel.books = it.realPath

            mDatabind.ivBusinessLicense.run {
                visible()
                load(it.path)
            }


        }

    }


    override fun initViewModelId(): Int = BR.cooperateViewModel

    override fun layoutId(): Int = R.layout.fragment_fleet_or_enterprise

    override fun initView(savedInstanceState: Bundle?) {

        mDatabind.initListener()

    }


    fun FragmentFleetOrEnterpriseBinding.initListener() {

        ivClickLeft.setOnClickListener(cardFrontListener)
        ivCardFront.setOnClickListener(cardFrontListener)
        tvDriverLicensePositive.setOnClickListener(cardFrontListener)

        ivClickRight.setOnClickListener(cardBackListener)
        ivCardBack.setOnClickListener(cardBackListener)
        tvDriverLicenseBack.setOnClickListener(cardBackListener)

        ivClick.setOnClickListener(licenseListener)
        ivBusinessLicense.setOnClickListener(licenseListener)
        tvBusinessLicense.setOnClickListener(licenseListener)





        btGetCode.setOnClickListener {


            mViewModel.mobile = stvPhone.centerEditValue
            mViewModel.onGetCode()
        }

        btnCommit.onClickIsLogin {

            mViewModel.run {

                names = stvName.centerEditValue
                address = stvAddress.centerEditValue
                contact = stvContact.centerEditValue
                mobile = stvPhone.centerEditValue
                code = stvCode.centerEditValue
                team_work()
            }
        }

    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.notify.observer(this) {

            mDatabind.clear()
            eventViewModel.onGoBackToMyEvent.value = 0

        }

    }

    private fun FragmentFleetOrEnterpriseBinding.clear() {
        stvName.setCenterEditString("")
        stvAddress.setCenterEditString("")
        stvContact.setCenterEditString("")
        stvPhone.setCenterEditString("")
        stvCode.setCenterEditString("")

        ivCardFront.invisible()
        ivCardBack.invisible()
        ivBusinessLicense.invisible()
        mViewModel.run {
            card_front = ""
            card_back = ""
            books = ""
        }
    }

    override fun initData() {
        super.initData()
        mViewModel.isCompany = arguments?.getBoolean("isCompany") ?: false
        mDatabind.btnCommit.text = if (mViewModel.isCompany) "企业合作" else "车队合作"
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
    }

    companion object {
        fun newInstance(isCompany: Boolean): FleetOrEnterpriseFragment {
            val args = Bundle()
            args.putBoolean("isCompany", isCompany)
            val fragment = FleetOrEnterpriseFragment()
            fragment.arguments = args
            return fragment
        }
    }
}